<template>
  <view>
    <l-background />
    <cert-form v-if="status" :type="type" :auth-data="authData" :auth-info="authInfo" @success="submit" />
    <cert-status v-else />
  </view>
</template>

<script>
import CertForm from '../components/cert-form/cert-form'
import CertStatus from '../components/cert-status/cert-status'
import { mapGetters } from 'vuex'
import { getAutoCompanyCert } from '@/common/api/user'
export default {
  name: 'Cert',
  components: {
    CertForm,
    CertStatus
  },
  data() {
    return {
      authData: '',
      authInfo: {},
      status: 1,
      type: 0 // 0:车企认证  1：旅行社用户登录认证
    }
  },
  computed: {
    ...mapGetters({
      userId: 'userId'
    })
  },
  onLoad(e) {
    if (e.type) this.type = e.type
    this.getAutoCompanyCert()
  },
  beforeDestroy() {
    this.back()
  },
  methods: {
    // 获取认证信息
    async getAutoCompanyCert() {
      const send = {
        userId: this.userId
      }
      const [err, res] = await getAutoCompanyCert(this.userId, send)
      if (err || res.statusCode !== 200) {
        this.toast(res.errMsg)
        return false
      }
      const data = res.data.data
      this.status = data.certStatus
      if (this.status === 1) {
        this.jump('/pages/order/order', 'switchTab')
      } else if (this.status === 2) {
        this.authData = '认证失败原因:' + ' ' + data.comment
        data.scopeid = JSON.parse(data.scopeid)
        if (!data.idCardValidDateEnd) data.idCardValidDateEnd = '长期'
        this.authInfo = data
        uni.setNavigationBarTitle({
          title: '审核结果'
        })
      } else if (this.status === 0) {
        uni.setNavigationBarTitle({
          title: '审核中'
        })
      }
    },
    // 返回
    back() {
      this.login.loginOut('/auth/authentication/removeToken')
    },
    //提交审核成功
    submit() {
      uni.setNavigationBarTitle({
        title: '审核中'
      })
      this.status = 0
    }
  }
}
</script>

<style scoped>

</style>
